package com.xiangmo.travel.Controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiangmo.travel.Annotation.TokenRequired;
import com.xiangmo.travel.Dto.CommunityDto;
import com.xiangmo.travel.Dto.ResultData;
import com.xiangmo.travel.Dvo.CommunityDvo;
import com.xiangmo.travel.Entity.Community;
import com.xiangmo.travel.Enums.CodeAndMsg;
import com.xiangmo.travel.Result.PageToListResult;
import com.xiangmo.travel.Service.CommunityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 社区板块
 */
@Slf4j
@RestController
@RequestMapping("/api/community")
public class CommunityController {
    @Autowired
    private CommunityService communityService;

    //根据类型查询社区数据 分页 可以考虑对数据进行封装
    @GetMapping("/type/page/{type}")
    public ResultData<PageToListResult<CommunityDvo>> getCommunityPage(@RequestParam int pageNum, @RequestParam int pageSize, @PathVariable String type){
        PageToListResult<CommunityDvo> community = communityService.getCommunityByPage(pageNum, pageSize, type);
        return new ResultData<>(CodeAndMsg.SUCCESS.getCode(), CodeAndMsg.SUCCESS.getMessage(),
                community, null);
    }

    //登录后 根据类型查询社区数据 分页 可以考虑对数据进行封装
    @TokenRequired
    @GetMapping("/type/page/{type}/{userId}")
    public ResultData<PageToListResult<CommunityDvo>> getCommunityPageByUserId(@RequestParam int pageNum, @RequestParam int pageSize,
                                                                               @PathVariable String type, @PathVariable Long userId){
        PageToListResult<CommunityDvo> community = communityService.getCommunityByPageAndUserId(userId, pageNum, pageSize, type);
        return new ResultData<>(CodeAndMsg.SUCCESS.getCode(), CodeAndMsg.SUCCESS.getMessage(),
                community, null);
    }

    //发布指定社区的一个文章
    @TokenRequired
    @PostMapping("/published")
    public ResultData<Community> setOneCommunity(@RequestBody CommunityDto communityDto,
                                                 @RequestHeader("Authorization") String token){
        int status = communityService.setOneCommunity(communityDto);
        if(status == 1){
            return new ResultData<>(CodeAndMsg.SUCCESS.getCode(), CodeAndMsg.SUCCESS.getMessage(),
                    null, token.substring(7));
        }
        log.warn("社区帖子发布失败！");
        return new ResultData<>(CodeAndMsg.ERROR.getCode(), CodeAndMsg.ERROR.getMessage(),
                null, token.substring(7));
    }

    //获取当前指定社区的总评论人数，也就是热度
    @TokenRequired
    @GetMapping("/count/{type}")
    public ResultData<Long> getCommunityPage(@PathVariable String type){
        return new ResultData<>(CodeAndMsg.SUCCESS.getCode(), CodeAndMsg.SUCCESS.getMessage(),
                communityService.getCommunityNum(type), null);
    }

    //根据社区的文章id查询文章的详情信息
    @TokenRequired
    @GetMapping("/article/{id}")
    public ResultData<CommunityDvo> getCommunityById(@PathVariable Long id, @RequestHeader("Authorization") String token){
        CommunityDvo communityDvo = communityService.getCommunityById(id);
        if(communityDvo == null){
            return new ResultData<>(CodeAndMsg.ATTRACTIONS_CONTENT_ERROR.getCode(),
                    CodeAndMsg.ATTRACTIONS_CONTENT_ERROR.getMessage(), null, token.substring(7));
        }
        return new ResultData<>(CodeAndMsg.SUCCESS.getCode(), CodeAndMsg.SUCCESS.getMessage(),
                communityDvo, token.substring(7));
    }
    //根据用户id查询该用户发过的帖子
    @TokenRequired
    @GetMapping("/user/community/{id}")
    public ResultData<Page<Community>> getCommunityUserPage(@RequestParam int pageNum, @RequestParam int pageSize, @PathVariable Long id,
                                                        @RequestHeader("Authorization") String token){
        Page<Community> community = communityService.getCommunityUserByPage(pageNum, pageSize, id);
        return new ResultData<>(CodeAndMsg.SUCCESS.getCode(), CodeAndMsg.SUCCESS.getMessage(),
                community, token.substring(7));
    }
    //删除帖子
    @TokenRequired
    @DeleteMapping("/delete/{id}")
    public ResultData<Page<Community>> deleteCommunityById(@PathVariable Long id, @RequestHeader("Authorization") String token){
        int status = communityService.deleteCommunityById(id);
        if(status == 1){
            return new ResultData<>(CodeAndMsg.DELETE_SUCCESS.getCode(), CodeAndMsg.DELETE_SUCCESS.getMessage(),
                    null, token.substring(7));
        }
        log.warn("社区删除帖子失败！");
        return new ResultData<>(CodeAndMsg.DELETE_ERROR.getCode(), CodeAndMsg.DELETE_ERROR.getMessage(),
                null, token.substring(7));
    }
}
